{include file="common/pc/header" title="$enterpriseModule.module.title - 权限设置" /}

<style>
    #authoritySetting {
        padding: 10px;
    }

    #authorityTable th {
        text-align: center;
    }

    #authoritySetting input[type='radio'][value='0'] + .layui-form-radioed .layui-icon {
        color: red;
    }

    #authoritySetting .radio-area {
        float: left;
    }
    #authoritySetting .cdu-area {
        margin-top: 110px;
    }
    #authoritySetting .cdu-area .chooseDeptUserBox {
        margin-top: 17px;
    }
</style>

<div id="authoritySetting">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">模块管理员</label>
            <div class="layui-input-block">
                {d:cdu name="adminers" dept="false" user="true" rootdept="1" value="$adminers"}请选择员工{/d:cdu}
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">模块可见性</label>
            <div class="layui-input-block">
                <input type="radio" name="accessers_type" lay-filter="accessers_type" value="adminOnly" title="仅管理员可见"/>
                <input type="radio" name="accessers_type" lay-filter="accessers_type" value="*" title="所有员工可见"/>
                <input type="radio" name="accessers_type" lay-filter="accessers_type" value="custom"
                       title="管理员及指定员工或部门可见"/>
                {php}
                $accessersList = isset($accessers['list']) ? $accessers['list'] : [];
                if(is_array($accessersList)) {
                    $accessersList = json_encode($accessersList);
                }
                {/php}
                {d:cdu name="accessers" dept="true" user="true" rootdept="1" id="accessers" value="$accessersList"}请选择员工或部门{/d:cdu}
                <script>
                    $("input[lay-filter='accessers_type']").each(function () {
                        if (this.value == '{$accessers.type}') {
                            this.checked = true;
                        }
                    });
                    if ('{$accessers.type}' != 'custom') {
                        $("#chooseDeptUserBox_accessers").hide();
                    }
                    layui.use('form', function () {
                        var form = layui.form;
                        form.on('radio(accessers_type)', function (data) {
                            var elem = $(data.othis).parent().find(".chooseDeptUserBox");
                            if (data.value != 'custom') {
                                elem.hide();
                            } else {
                                elem.show();
                            }
                        });
                    });
                </script>
            </div>
        </div>
        {if condition="!empty($groupAuthorities)"}
        <table class="layui-table" id="authorityTable">
            <thead>
            <tr>
                <th colspan="5">功能权限设置</th>
            </tr>
            <tr>
                <th colspan="2" width="20%">功能</th>
                <th width="10%">企业管理员</th>
                <th width="10%">模块管理员</th>
                <th width="60%">普通员工</th>
            </tr>
            </thead>
            <tbody>
            {php}$idx = 1;{/php}
            {foreach name="groupAuthorities" item="groupAuthority" key="groupName"}
            {foreach name="groupAuthority" key="key" item="authority"}
            <tr>
                <td>{$groupName}</td>
                <td>
                    <input name="key[]" type="hidden" value="{$key}"/>
                    {$authority.title}
                </td>
                <td align="center">
                    <input type="radio" name="entAdmin_{$idx}" lay-filter="entAdmin_{$idx}" value="0" title="无权限"/>
                    <input type="radio" name="entAdmin_{$idx}" lay-filter="entAdmin_{$idx}" value="1" title="有权限"/>
                    <script>
                        $("input[lay-filter='entAdmin_{$idx}']").each(function () {
                            if (this.value == '{$authority.value.entAdmin}') {
                                this.checked = true;
                            }
                        });
                    </script>
                </td>
                <td align="center">
                    <input type="radio" name="moduleAdmin_{$idx}" lay-filter="moduleAdmin_{$idx}" value="0" title="无权限"/>
                    <input type="radio" name="moduleAdmin_{$idx}" lay-filter="moduleAdmin_{$idx}" value="1" title="有权限"/>
                    <script>
                        $("input[lay-filter='moduleAdmin_{$idx}']").each(function () {
                            if (this.value == '{$authority.value.moduleAdmin}') {
                                this.checked = true;
                            }
                        });
                    </script>
                </td>
                <td>
                    <div class="radio-area">
                        <input type="radio" name="other_{$idx}" lay-filter="other_{$idx}" value="0" title="所有员工都无权限"/><br/>
                        <input type="radio" name="other_{$idx}" lay-filter="other_{$idx}" value="*" title="所有员工都有权限"/><br/>
                        <input type="radio" name="other_{$idx}" lay-filter="other_{$idx}" value="1" title="以下员工有权限"/>
                    </div>
                    <div class="cdu-area">
                        {php}$fieldName = 'other_cdu_'.$idx;{/php}
                        {d:cdu name="$fieldName" dept="true" user="true" rootdept="1" id="$fieldName"}请选择允许访问的员工或部门{/d:cdu}
                        {if condition="$authority.value.other && $authority.value.other != '*'"}
                        <script>
                            $("input[lay-filter='other_{$idx}'][value='1']").attr("checked", true);
                            $(document).ready(function () {
                                g.cdu.set('{$fieldName}', '{$authority.value.other}');
                            });
                        </script>
                        {else/}
                        <script>
                            $("input[lay-filter='other_{$idx}'][value='{$authority.value.other}']").attr("checked", true);
                            $("#chooseDeptUserBox_{$fieldName}").hide();
                        </script>
                        {/if}
                    </div>
                    <script>
                        layui.use('form', function () {
                            var form = layui.form;
                            form.on('radio(other_{$idx})', function (data) {
                                var elem = $(data.othis).parent().parent().find(".chooseDeptUserBox");
                                if (data.value != '1') {
                                    elem.hide();
                                } else {
                                    elem.show();
                                }
                            });
                        });
                    </script>
                </td>
                {php}$idx++;{/php}
            </tr>
            {/foreach}
            {/foreach}
            </tbody>
        </table>
        {/if}
        <div class="layui-form-item">
            <button class="layui-btn" lay-submit lay-filter="submit"> 保 存</button>
        </div>
    </form>
</div>

<script>
    layui.use('form', function () {
        var form = layui.form;
        form.render();

        form.on("submit(submit)", function (data) {
            console.log(data);
            g.loading();
            $.post(document.location.href, data.field, function (ret) {
                g.loading(false);
                if (ret.code == 1) {
                    layer.msg(ret.msg);
                    document.location.reload();
                } else {
                    layer.alert(ret.msg);
                }
            }, "json");
            return false;
        });
    });

    // 将相同组名的归为一组
    var groupNameTds = {};
    $("#authorityTable tbody tr").each(function () {
        var $td = $("td", this).first();
        var groupName = $td.text();
        if (!groupNameTds[groupName]) {
            groupNameTds[groupName] = [];
        }
        groupNameTds[groupName].push($td);
    });
    for (var groupName in groupNameTds) {
        var tds = groupNameTds[groupName];
        tds[0].attr("rowspan", tds.length);
        for (var i = 1; i < tds.length; i++) {
            tds[i].remove();
        }
    }
</script>

{include file="common/pc/footer"/}